from urllib.request import urlopen
url="http://www.baidu.com"
resp = urlopen(url)
try:
    # 使用 with 语句自动管理网络响应的生命周期
    with urlopen(url) as resp:
        html_content = resp.read().decode("utf-8")
    with open("mybaidu.html","w",encoding="utf-8")as f:
        f.write(html_content)
    print("over")
except Exception as result:
    print(f"出现错误：{result}")